package database.helpers;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class VOTD_Table {

public databas_helper dbHelper;	
public SQLiteDatabase database;
	/**
	 * Assignment table related constants.
	 */
	public final static String VOTD_TABLE="VOTD";	
	public final static String VOTD_Day="Day";
	public final static String VOTD_REFSCRIPT="Reference";
	public final static String VOTD_CONTENT="CONTENT";
	public final static String VOTD_wasREAD="wasREAD";
	public final static String VOTD_ID="_id";
	
	public VOTD_Table(Context context){
		dbHelper = new databas_helper(context);
		database = dbHelper.getWritableDatabase();
	}
	public VOTD_Table(databas_helper dbHelper,SQLiteDatabase database){
		this.dbHelper = dbHelper;
		this.database = database;
	}
	
	/**\
	 * Creates a new VOTD
	 	 */
	public long addVOTD(String Day, String refScript, String CONTENT, String wasREAD){
		ContentValues values = new ContentValues();
		values.put(VOTD_Day,Day);
		values.put(VOTD_REFSCRIPT,refScript);
		values.put(VOTD_CONTENT,CONTENT);
		values.put(VOTD_wasREAD,wasREAD);

		return database.insert(VOTD_TABLE, null, values);
	}	
	
	public void close() {
		dbHelper.close();
	}
	/**
	 * Fetch all VOTDs
	 * @return
	 */
	public Cursor getAllVOTDs(){
		Cursor mCursor = database.query(true, VOTD_TABLE, new String[]
				{
				VOTD_ID,VOTD_Day, VOTD_REFSCRIPT,VOTD_CONTENT, VOTD_wasREAD},null
				, null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}
	/**
	 * 
	 */
	public int editVOTD(String Day, String refScript, String CONTENT, String wasREAD){
		ContentValues values = new ContentValues();
		values.put(VOTD_Day,Day);
		values.put(VOTD_REFSCRIPT,refScript);
		values.put(VOTD_CONTENT,CONTENT);
		values.put(VOTD_wasREAD,wasREAD);
		
		return database.update(VOTD_TABLE, values,VOTD_ID + "=?",new String[]{Day});
	}
	public int UpdateReadStatus(String wasread, String id){
		ContentValues values = new ContentValues();
		if(wasread.equalsIgnoreCase("false"))
		{
			values.put(VOTD_wasREAD,"true");
			values.put(VOTD_Day,"today");
		}
		else
			values.put(VOTD_wasREAD,"false");
		
		return database.update(VOTD_TABLE, values, VOTD_ID + "='"+id+"'", null) ;	
	}
	public int UpdateOpened(String wasread, String id){
		ContentValues values = new ContentValues();
		if(wasread.equalsIgnoreCase("today"))
		{			
			values.put(VOTD_Day,"done");
		}
				
		return database.update(VOTD_TABLE, values, VOTD_ID + "='"+id+"'", null) ;	
	}
	
	public boolean delete(String Day){
		return database.delete(VOTD_TABLE, VOTD_ID + "=" + Day, null) > 0;
	}
	public boolean isEmpty() {
		int count;
		Cursor c= getAllVOTDs();
		count= c.getCount();
		c.close();
		if (count>0)
			return false;
		// TODO Auto-generated method stub
		return true;
	}
}
